package com.adl.telawa;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.adl.telawa.utils.WatchDog;

/**
 * @author Adlane Achab.
 */
public class Initial extends Activity implements OnClickListener
{
	private ImageButton pauseButton;
	private ImageButton playButton;
	private ImageButton nextButton;
	private ImageButton previousButton;
	private ImageButton forwardButton;
	private ImageButton backwardButton;
	private ImageButton english;
	private ImageButton arabic;

	protected TextView label;
	private ProgressBar progressBar;
	protected ExpandableListView list;

	private boolean _init = false;

	protected final Streamer _streamer = new Streamer();

	public static  Initial INSTANCE = null;
	public Initial(){ INSTANCE = this;}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);

		initControls();

		WatchDog.INSTANCE.setConnectivityManager((ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE));
	}

	public void info(final String text)
	{
		try
		{
			runOnUiThread(new Runnable() {
				public void run() {
					Toast.makeText(Initial.INSTANCE, text, Toast.LENGTH_LONG).show();
				}
			});
		}
		catch (Throwable e) {}
	}

	private void initControls()
	{
		label = (TextView) findViewById(R.id.label);
		pauseButton = (ImageButton) findViewById(R.id.button_pause);
		playButton = (ImageButton) findViewById(R.id.button_play);
		nextButton = (ImageButton) findViewById(R.id.button_next);
		previousButton = (ImageButton) findViewById(R.id.button_previous);
		forwardButton = (ImageButton) findViewById(R.id.button_forward);
		backwardButton = (ImageButton) findViewById(R.id.button_backward);
		arabic = (ImageButton) findViewById(R.id.arabic);
		english = (ImageButton) findViewById(R.id.english);
		progressBar = (ProgressBar) findViewById(R.id.progress_bar);
		list = (ExpandableListView) findViewById(android.R.id.list);

		MyListAdapter.INSTANCE.setContext(this);
		list.setAdapter(MyListAdapter.INSTANCE);
		list.setOnChildClickListener(MyListAdapter.INSTANCE);
		list.setOnGroupExpandListener(MyListAdapter.INSTANCE);

		pauseButton.setOnClickListener(this);
		playButton.setOnClickListener(this);
		nextButton.setOnClickListener(this);
		previousButton.setOnClickListener(this);
		forwardButton.setOnClickListener(this);
		backwardButton.setOnClickListener(this);
		arabic.setOnClickListener(this);
		english.setOnClickListener(this);

		_streamer.init(progressBar);
	}

	protected void startStreamingAudio(final String url, final int debit)
	{	
		_init = true;

		try { 
			_streamer.startStreaming(url);
		}
		catch (Throwable e) {Log.e(getClass().getName(), "Error starting to stream audio.", e);}
	}

	private static final int EXIT = 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, EXIT, 0, "Exit");
		return true;
	}

	public boolean onOptionsItemSelected (MenuItem item)
	{
		switch (item.getItemId()){
		case EXIT:
			_streamer.stop();
			finish();
			return true;
		}
		return false;
	}

	@Override
	public void onClick(View button)
	{
		if (button == pauseButton)
		{
			_streamer.pause();
		}
		else if (button == playButton)
		{
			if (_init == true)
				_streamer.start();
			else
				MyListAdapter.INSTANCE.playCurrentSurat();
		}
		else if (button == nextButton)
		{
			MyListAdapter.INSTANCE.nextSurat();
		}
		else if (button == previousButton)
		{
			MyListAdapter.INSTANCE.prevoiusSurat();
		}
		else if (button == arabic)
		{
			MyListAdapter.INSTANCE.setLanguage(MyListAdapter.ARABIC);			
		}
		else if (button == english)
		{
			MyListAdapter.INSTANCE.setLanguage(MyListAdapter.ENGLISH);
		}
		else if (button == backwardButton)
		{
			_streamer.backward();
		}
		else if (button == forwardButton)
		{
			_streamer.forward();
		}
	}
}
